from fastapi import APIRouter, Depends
from fastapi.encoders import jsonable_encoder
from sqlalchemy.orm import Session
import xmlrpc.client as xmlrpclib
from supervisor import xmlrpc
import models
from models.db import get_db
from schemas import response_success
from supervisor.options import UnhosedConfigParser

router = service_router = APIRouter()


@router.get("")
async def get_list(db: Session = Depends(get_db)):
    fp = open('config/asm.conf', 'r')
    parser = UnhosedConfigParser()
    parser.read_file(fp)
    fp.close()

    server_url = parser.get('supervisorctl', 'serverurl')
    server_proxy = xmlrpclib.ServerProxy('http://127.0.0.1', transport=xmlrpc.SupervisorTransport(serverurl=server_url))
    supervisor = getattr(server_proxy, 'supervisor')
    all_infos = supervisor.getAllProcessInfo()
    return response_success(data={'list': all_infos})
